package com.qianf.qfoffice.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qianf.qfoffice.entity.Course;
import com.qianf.qfoffice.entity.ResponseData;
import com.qianf.qfoffice.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseController {

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(true);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }


    @Autowired
    private CourseService courseService;

    /**
     * 判断当前用户是否 登录成功
     * @return
     */
    @RequestMapping("/coursepage.do")
    public ResponseData<Object> coursepage(int page, int limit){
        // 开启分页
        PageHelper.startPage(page, limit);

        List<Course> courseList = courseService.findAllCourse();

        PageInfo pageInfo = new PageInfo(courseList);

        ResponseData<Object> responseData = new ResponseData<>();

        // ######### 必须是0 才代表响应成功
        responseData.setCode(0);
        responseData.setMsg("查询失败");
        responseData.setCount((int) pageInfo.getTotal());
        responseData.setData(courseList);

        return responseData;

    }

    @RequestMapping("/coursedelete.do")
    public ResponseData coursedelete(int id) {
        ResponseData<Object> responseData = new ResponseData<>();

        int num = courseService.deleteCourseById(id);
        if (num > 0){
            responseData.setCode(1);
            responseData.setMsg("删除成功");
            return responseData;
        }else {
            responseData.setCode(0);
            responseData.setMsg("删除失败");
            return responseData;
        }


    }
    @RequestMapping("/courseupdate.do")
    public ResponseData courseupdate(Course course){
        ResponseData<Object> responseData = new ResponseData<>();
//        int  num =   studentService.updateStudentById(id);
        int num = courseService.updateCourse(course);
        if (num>0){
            responseData.setCode(1);
            return responseData;
        }else {
            responseData.setCode(0);
            return responseData;
        }
    }
    @RequestMapping("/addCourse.do")
    public ResponseData addCourse(Course course){
        ResponseData<Object> responseData = new ResponseData<>();
//        int  num =   studentService.updateStudentById(id);
        int num = courseService.addCourse(course);
        if (num>0){
            responseData.setCode(1);
            return responseData;
        }else {
            responseData.setCode(0);
            return responseData;
        }
    }


}
